package com.example.demo.Controller;
import com.alibaba.fastjson.JSON;
import com.example.demo.Dao.AnswerDao;
import com.example.demo.domin.Answer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@CrossOrigin
@RestController
public class AnswerController {
    @Autowired
    AnswerDao answerDao;


    @RequestMapping(value = "/saveAnswer",method = RequestMethod.POST)
    public String saveAnswer(@RequestBody List<Answer> answer) {//传来的数据转换为对象json->obj
        System.out.println("*****Answer******:" + answer.toString());
        answerDao.saveAll(answer);
        return null;
    }


    @RequestMapping(value = "/statistics",method = RequestMethod.GET)
    public String statistics(Long formId) {
        System.out.println("*****formId******:" + formId);
        int i = answerDao.getCounts(formId);
        System.out.println("*****Count******:" + i);
        List list = answerDao.getQuestionCounts(formId);
        System.out.println("*****List******:" + list.size());
        HashMap<String,Object> res=new HashMap<String, Object>();//放入map集合K:V
        res.put("peopleCounts", i);
        res.put("optionCounts",list);//收容传来的对象
        String res_json= JSON.toJSONString(res);//把一个对象转换为json串:把product对象转变成json数据
        System.out.println(res_json);
        return res_json;
    }
}
